package com.hu.zhcc.shiro.controller.Lixueseng520;

import com.hu.zhcc.common.entity.Page;
import com.hu.zhcc.shiro.entity.bo.Lixueseng520.Suggest;
import com.hu.zhcc.shiro.service.Lixueseng520.SuggestService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("suggest")
public class SuggestController {
    @Autowired
    private SuggestService suggestService;
    // 查询
    @GetMapping("")
    public Page<Suggest> getSuggestList(String name, Integer offset, Integer limit){
        Map map = new HashMap();
        if (!StringUtils.isBlank(name)) {
            map.put("name",name);
        }
        return suggestService.listSuggest(map,offset,limit);
    }
    // 添加
    @PostMapping("add")
    public int saveSuggest(@RequestBody Suggest suggest){
        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyy-MM-dd HH:mm:ss");
        String creationTime = format.format(date);
        suggest.setCreationtime(creationTime);
        return suggestService.insertSelective(suggest);
    }
    // 修改
    @PutMapping("/{id}")
    public int getSuggestById(@PathVariable("id") int id,@RequestBody Suggest suggest){
        System.out.println(suggest);
        return suggestService.updateByPrimaryKeySelective(suggest);
    }
    // 删除
    @DeleteMapping("/{id}")
    public int removeSuggest(@PathVariable("id") int id){
        return suggestService.deleteByPrimaryKey(id);
    }
}
